import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_li_ke_jie/ui/auth/real_name_model.dart';
import 'package:flutter_li_ke_jie/utils/img_util.dart';
import 'package:flutter_li_ke_jie/utils/navigator_util.dart';

import '../../core/interface/common_interface.dart';
import '../../style/common_color.dart';

/// 实名认证
class RealNamePage extends StatefulWidget {
  const RealNamePage({Key? key}) : super(key: key);

  @override
  _RealNamePageState createState() => _RealNamePageState();
}

class _RealNamePageState extends State<RealNamePage> with CommonInterface<RealNameModel> {
  late RealNameModel _realNameModel;
  late TextEditingController _nameController;
  late TextEditingController _phoneController;
  late TextEditingController _cardIdController;

  @override
  void initState() {
    _nameController = TextEditingController();
    _phoneController = TextEditingController();
    _cardIdController = TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _realNameModel = model(context);
    return WillPopScope(
        child: Scaffold(
            resizeToAvoidBottomInset: false,
            appBar:_appBar(),
            body: Container(
              color: color_f7,
              child: Column(
                children: [
                  Expanded(child: ListView(
                    padding: EdgeInsets.only(top: 0,left: 20,right: 20),
                    children: _content(),
                  ),flex: 1,),
                  _bottomBox()
                ],
              ) ,
            )
        ),
        onWillPop: ()=>_exitPage()
    );
  }

  //退出页面
  Future<bool> _exitPage() {
    _showBackDialog();
    return Future.value(false);
  }


  //appabr
  AppBar _appBar(){
    return AppBar(
      backgroundColor: Colors.white,
      elevation: 0,
      title: Text("身份认证"),
      centerTitle: true,
      titleTextStyle: TextStyle(color: color_1e,fontSize: 18,fontWeight: FontWeight.bold),
      leading: Padding(
          padding: EdgeInsets.only(left: 10),
          child: GestureDetector(
            onTap: (){
              _showBackDialog();
              // NavigatorUtil.pop(context);
            },
            child: Image.asset(ImageUtil.loadLocalImg("icon_back.png"),height: 30,width: 30,),
          )
      ),
      leadingWidth: 40,
    );
  }

  //主题内容
  List<Widget> _content(){
    return [
      SizedBox(height: 28,),
      Image.asset(ImageUtil.loadLocalImg("icon_id.png"),height: 80,width: 80,),
      SizedBox(height: 12,),
      Text("请正确输入姓名和身份证号，以便核实您身份的真实性，认证通过后不可进行修改",textAlign: TextAlign.center,style: TextStyle(color: color_1e,fontSize: 16),),
      SizedBox(height: 28,),
      Container(

        padding: EdgeInsets.only(left: 20,right: 20),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10))
        ),
        child: Column(
          children: [
            ///手机号
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(child: Text("手机号",style: TextStyle(color: color_1e,fontSize: 16),),flex: 2,),
                SizedBox(width: 10,),
                Expanded(child: TextField(
                  controller: _phoneController,
                  keyboardType: TextInputType.number,
                  maxLength: 11,
                  maxLines: 1,
                  cursorColor: themeColor,
                  style: TextStyle(fontSize: 18,color: color_1e),
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.all(0),
                    counterText: "",
                    hintText: "请输入手机号",
                    hintStyle:  TextStyle(fontSize: 16),
                    border:InputBorder.none,
                  ),
                ),flex: 5,)
              ],
            ),
            Container(height: 1,color: color_eee,),

            ///姓名
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(child: Text("姓名",style: TextStyle(color: color_1e,fontSize: 16),),flex: 2,),
                SizedBox(width: 10,),
                Expanded(child: TextField(
                  controller: _nameController,
                  maxLength: 30,
                  maxLines: 1,
                  cursorColor: themeColor,
                  style: TextStyle(fontSize: 18,color: color_1e),
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.all(0),
                    counterText: "",
                    hintText: "请输入姓名",
                    hintStyle:  TextStyle(fontSize: 16),
                    border:InputBorder.none,
                  ),
                ),flex: 5,)
              ],
            ),
            Container(height: 1,color: color_eee,),

            ///身份证号码
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(child: Text("身份证号码",style: TextStyle(color: color_1e,fontSize: 16),),flex: 2,),
                SizedBox(width: 10,),
                Expanded(child: TextField(
                  controller: _cardIdController,
                  inputFormatters: [
                    FilteringTextInputFormatter(RegExp("^[a-z0-9A-Z]+"), allow: true),//只允许输入数字，字母
                  ],
                  maxLength: 18,
                  maxLines: 1,
                  cursorColor: themeColor,
                  style: TextStyle(fontSize: 18,color: color_1e),
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.all(0),
                    counterText: "",
                    hintText: "请输入身份证号码",
                    hintStyle:  TextStyle(fontSize: 16),
                    border:InputBorder.none,
                  ),
                ),flex: 5,)
              ],
            ),
          ],
        ),
      ),
      SizedBox(height: 10,),
      Text("*请如实填写身份信息",style: TextStyle(color: errorColor,fontSize: 16),)
    ];
  }

  //底部
  Widget _bottomBox(){
    return Container(
      color: Colors.white,
      width: double.infinity,
      padding: EdgeInsets.all(20),
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(ImageUtil.loadLocalImg("icon_safe.png"),height: 20,width: 20,),
              SizedBox(width: 5,),
              Text("数据加密保护，仅用于贷款审核",style: TextStyle(color: color_999,fontSize: 16),),
            ],
          ),
          SizedBox(height: 10,),
          //提交资料
          MaterialButton(
            height: 50,
            minWidth: double.infinity,
            color: themeColor,
            shape:const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ) ,
            onPressed: (){
              var future = _realNameModel.saveUser(_nameController.text, _phoneController.text, _cardIdController.text);
              future.then((success){
                if(success){
                  _showSuccessDialog();
                }
              });

            },
            child: Text("下一步",style: TextStyle(color: Colors.white,fontSize: 16),),
          ),
        ],
      ),
    );
  }

  //返回弹窗
  void _showBackDialog(){
    showDialog(
        context: context,
        builder: (BuildContext buildContext){
          return AlertDialog(
            title: Text("资料未填完，确认退出吗?",textAlign: TextAlign.center,),
            titleTextStyle: TextStyle(color: color_1e,fontSize: 18),
            content: Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  MaterialButton(
                    onPressed:(){
                      NavigatorUtil.pop(buildContext);
                      NavigatorUtil.pop(context);
                    },
                    color: color_eee,
                    child: Text("退出",style: TextStyle(color: color_bf,fontSize: 16),),),
                  MaterialButton(
                    onPressed:(){
                      NavigatorUtil.pop(buildContext);
                    },
                    color: themeColor,
                    child: Text("继续填写",style: TextStyle(color: Colors.white,fontSize: 16),),),
                ],
              )
            ),
          );
        });
  }

  //提交成功弹窗
  void _showSuccessDialog(){
    showDialog(
        context: context,
        barrierDismissible: false,//空白不关闭
        builder: (BuildContext buildContext){
          return AlertDialog(
            title: Text("资料提交成功",textAlign: TextAlign.center,),
            titleTextStyle: TextStyle(color: color_1e,fontSize: 18),
            content: Container(
              width: double.infinity,
              padding: EdgeInsets.only(left: 10,right: 10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Image.asset(ImageUtil.loadLocalImg("icon_success.png"),height: 80,width: 80,),
                  SizedBox(height: 20,),
                  MaterialButton(
                    minWidth: double.infinity,
                    height: 40,
                    onPressed:(){
                      NavigatorUtil.pop(buildContext);
                      NavigatorUtil.pop(context);
                    },
                    color: themeColor,
                    child: Text("返回首页",style: TextStyle(color: Colors.white,fontSize: 16),),),
                ],
              )
            ),
          );
        });
  }
}
